
package combat.dudes.client.gui.threads.shell;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Toolkit;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLayeredPane;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import combat.dudes.DudesFunctions;
import combat.dudes.client.ClientApp;
import combat.dudes.client.gui.ClientGUI;
import combat.dudes.client.gui.ClientGUIInternalWindow;
import combat.dudes.client.gui.ClientGUITheme;


/**
 * @author Laurens Andriessen
 * @version 1.0
 */
public class GUIShell {

	public static JFrame content = new JFrame(); // The actual application shell
	private static JDesktopPane desktop = new JDesktopPane(); // The desktop/pane within this shell to which the internal frames are added

	// Internal frame padding
	public static int pad = 3;

	// List of all internal frames (JInternalFrame (ClientGUIInternalWindow extends this))
	public static JInternalFrame windowLogin = new GUIShellLogin();
	public static ClientGUIInternalWindow windowWorldView = new ClientGUIInternalWindow(0, "World View", "world.png", ClientGUI.panelMainMap);
	public static ClientGUIInternalWindow windowMiniMap = new ClientGUIInternalWindow(1, "Minimap", "map.png", ClientGUI.panelMiniMap);
	public static ClientGUIInternalWindow windowInfo = new ClientGUIInternalWindow(2, "Information", "information.png", ClientGUI.panelInfo);
	public static ClientGUIInternalWindow windowMyStatus = new ClientGUIInternalWindow(4, "My stats", "user.png", ClientGUI.panelMyStatus);
	public static ClientGUIInternalWindow windowChat = new ClientGUIInternalWindow(3, "Chat", "chat.png", ClientGUI.panelChat);
	public static ClientGUIInternalWindow[] windows = { windowWorldView, windowMiniMap, windowInfo, windowMyStatus, windowChat };

	public static JMenuBar menu = new GUIShellMenu();


	public GUIShell() {


		// Set the theme
		ClientGUITheme.setGUILookAndFeel(ClientGUITheme.GUILaF.DARK);


		// Set up the shell
		setContentPane();


		// Add the background
		addBackground();


		// Add menu
		content.setJMenuBar(menu);


		// Sleep for a very short while, so the menu can be added and
		// the right 'size' of the desktop is being filled in below.
		try {
			Thread.sleep(50);
		} catch(Exception e) {
			ClientApp.onError("", e, true);
		}


		int winW = desktop.getWidth();
		int winH = desktop.getHeight();

		windowMiniMap.setSize( // x, y, w, h
				winW - pad - 260,
				pad,
				260,
				200);
		windowInfo.setSize(
				windowMiniMap.getBounds().x,
				windowMiniMap.getBounds().y + windowMiniMap.getBounds().height + pad,
				windowMiniMap.getBounds().width,
				winH - pad * 2 - windowMiniMap.getBounds().y - windowMiniMap.getBounds().height);
		windowMyStatus.setSize(
				windowInfo.getBounds().x - pad * 4 - 550,
				winH - pad * 4 - 175,
				550,
				175);
		windowChat.setSize(
				pad * 4,
				windowMyStatus.getBounds().y,
				400,
				175);
		// Last, because it's dynamic!
		windowWorldView.setSize(
				pad,
				pad,
				windowMiniMap.getBounds().x - pad * 2,
				// windowChat.getBounds().y - pad * 2);
				winH - pad * 2);


		// Show login screen
		desktop.add(windowLogin, JLayeredPane.PALETTE_LAYER);


		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				// Add (but hide, log in first) windows and make sure all added
				// windows are 'layered' (when focused, show them in front of other windows)
				for(ClientGUIInternalWindow window : windows) {
					if(window.id == 0)
						desktop.add(window, JLayeredPane.DEFAULT_LAYER);
					else desktop.add(window, JLayeredPane.PALETTE_LAYER);
					window.setVisible(false);
				}


				// Focus login window
				windowLogin.requestFocus();


				SwingUtilities.updateComponentTreeUI(content);
			}
		});

	}


	private static void setContentPane() {
		content.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		content.setTitle(ClientGUI.guiShellTitle);
		// Set application icon
		content.setIconImage(DudesFunctions.File.getIcon("forcevision_logoicon.png"));
		content.setResizable(false); // Disable window to be resized
		content.pack(); // Prepare all elements to be shown
		content.setVisible(true); // Show screen

		/* content.setSize(windowSizeWidth, windowSizeHeight - 40); // Set
		 * window size: 1250x750 // Center position on screen Dimension
		 * screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int
		 * locationX = (screenSize.width/2 - content.getWidth()/2); int
		 * locationY = (screenSize.height/2 - content.getHeight()/2);
		 * content.setLocation(locationX, locationY); */

		content.setMinimumSize(new Dimension(ClientGUI.guiShellWidth, ClientGUI.guiShellHeight));
		content.setExtendedState(content.getExtendedState() | JFrame.MAXIMIZED_BOTH);

		desktop.setDragMode(JDesktopPane.LIVE_DRAG_MODE);
		content.add(desktop, BorderLayout.CENTER);
		desktop.setVisible(true);

	}


	private static void addBackground() {
		// desktop.setBackground(new Color(196, 199, 206));
		// desktop.setEnabled(false);

		// JInternalFrame(title, resizable, closable, maximizable, iconifiable)
		JInternalFrame background = new JInternalFrame("Background", false, false, false, false);
		// Hide border
		background.setBounds(-6, -25, Toolkit.getDefaultToolkit().getScreenSize().width + 12, Toolkit.getDefaultToolkit().getScreenSize().height);
		background.setVisible(true);
		background.setFocusable(false);
		background.setEnabled(false);
		background.setIgnoreRepaint(true);
		try {
			background.setIcon(false);
			background.setSelected(false);
		} catch(Exception e) {}

		JPanel backgroundPanel = new JPanel() {
			private static final long serialVersionUID = 1L;
			@Override
			public void paintComponent(Graphics g) {
				if(ClientGUITheme.Background.showBgImage) {
					Image img = ClientGUITheme.Background.bgImage;
					// int W = 100, H = 100; // Manual
					// int W = this.getWidth(), H = this.getHeight(); // Dynamic
					// int W = Toolkit.getDefaultToolkit().getScreenSize().width, H = Toolkit.getDefaultToolkit().getScreenSize().height; // Fill entire desktop
					int W = img.getWidth(this), H = img.getHeight(this); // 1:1

					// int X = 0, Y = 0; // 0,0
					/* Center screen */
					int X = Toolkit.getDefaultToolkit().getScreenSize().width / 2 - img.getWidth(this) / 2;
					int Y = Toolkit.getDefaultToolkit().getScreenSize().height / 2 - img.getHeight(this) / 2;

					g.drawImage(img, X, Y, W, H, this);
				} else {
					Graphics2D g2 = (Graphics2D) g; // Make type Graphics2D
					g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Set AA on
					g2.setPaint(ClientGUITheme.Background.gradientLight); // Set default paint color on the light variant
					g2.setPaint(new GradientPaint(0, 0, ClientGUITheme.Background.gradientLight, ClientGUI.guiShellWidth, ClientGUI.guiShellHeight, ClientGUITheme.Background.gradientDark));
					g2.fill(new Rectangle.Double(0, 0, ClientGUI.guiShellWidth, ClientGUI.guiShellHeight)); // Draw gradient (defined on previous line) on screen
				}
			}
		};
		backgroundPanel.setFocusable(false);
		backgroundPanel.setEnabled(false);
		backgroundPanel.setIgnoreRepaint(true);
		// Remove popup menu (right mouse click)
		/* backgroundPanel.addMouseListener(new MouseListener() {
		 * public void mouseClicked(MouseEvent arg0) {}
		 * public void mouseEntered(MouseEvent arg0) {}
		 * public void mouseExited(MouseEvent arg0) {}
		 * public void mousePressed(MouseEvent arg0) {}
		 * public void mouseReleased(MouseEvent arg0) {}
		 * });
		 * JPopupMenu popup = background.getComponentPopupMenu();
		 * if(popup != null) popup.setSize(0, 0); */
		backgroundPanel.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(Toolkit.getDefaultToolkit().getImage(""), new Point(0, 0), "cursor"));

		background.add(backgroundPanel, BorderLayout.CENTER);
		desktop.add(background, JLayeredPane.DEFAULT_LAYER);
	}
}
